package com.xxl.shoppingapp.ui.user;


import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.xxl.baselib.baseui.widget.ClearEditText;
import com.xxl.baselib.baseui.widget.RoundButton;
import com.xxl.baselib.baseui.widget.TextWatcher;
import com.xxl.shoppingapp.R;
import com.xxl.shoppingapp.base.BaseActivity;
import com.xxl.shoppingapp.entity.BaseResponse;
import com.xxl.shoppingapp.http.ResponseListener;
import com.xxl.shoppingapp.http.ShoppingRequest;
import com.xxl.shoppingapp.ui.ShopAppUIHelper;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * @author xuelian.xiong 2018/4/9
 * @Package com.xxl.shoppingapp.ui
 * @Title: LoginActivity
 * @Description: (用一句话描述该文件做什么)
 * Create DateTime: 2018/4/9
 */
public class LoginActivity extends BaseActivity {
    @Bind(R.id.iv_photo)
    ImageView ivPhoto;
    @Bind(R.id.et_account)
    ClearEditText etAccount;
    @Bind(R.id.iv_expand)
    ImageView ivExpand;
    @Bind(R.id.et_password)
    ClearEditText etPassword;
    @Bind(R.id.iv_visible)
    ImageView ivVisible;
    @Bind(R.id.view)
    View view;
    @Bind(R.id.bt_login)
    RoundButton btLogin;
    @Bind(R.id.tv_register)
    TextView tvRegister;
    @Bind(R.id.tv_forgot_password)
    TextView tvForgotPassword;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("登录");
        etAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                setLoginEnable();
            }
        });
        etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                setLoginEnable();
            }
        });
//        setHasBack(true);
    }


    /**
     * 设置登录按钮点击状态
     */
    private void setLoginEnable() {
        if (TextUtils.isEmpty(etAccount.getText().toString()) || TextUtils.isEmpty(etPassword.getText().toString())) {
            btLogin.setEnabled(false);
        } else {
            btLogin.setEnabled(true);
        }
    }

    @OnClick(R.id.bt_login)
    public void onViewClicked() {
        ShoppingRequest.login(etAccount.getText().toString(), etPassword.getText().toString(), new ResponseListener() {
            @Override
            public void onSuccess(String response) {
                ShopAppUIHelper.navMain(LoginActivity.this);
            }

            @Override
            public void onFailed(String error) {

            }

            @Override
            public void onFinish() {

            }
        });
    }

    @OnClick(R.id.tv_register)
    public void onRegister() {
        ShopAppUIHelper.navRegister(LoginActivity.this);
    }
}